package com.mytime.feeds;

public class EventsFeed {

/*	private static String request = "http://api.eventful.com/rest/events/search?app_key=KJN78z24nttG7z2s";

	public static JSONObject getData(String location, String category) {
		
		if (location != null) {
			location = location.replaceAll(" ", "+");
			request += "&location=" + location;
		}

		if (category != null) {
			category = category.replaceAll(" ", "+");
			request += "&category=" + category;
		}

		JSONObject result = new JSONObject();
		
		try {
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new URL(request).openStream());
			
			XPathFactory factory = XPathFactory.newInstance();
			XPath xPath = factory.newXPath();

			//Get all search Result nodes
			NodeList nodes = (NodeList) xPath.evaluate("search/events/event", doc, XPathConstants.NODESET);
			int nodeCount = nodes.getLength();

			JSONArray venues = new JSONArray();
			JSONArray events = new JSONArray();
			
			//iterate over search Result nodes
			for (int i = 0; i < nodeCount; i++) {
				Node titleNode = (Node) xPath.evaluate("title", nodes.item(i), XPathConstants.NODE);
				Node descriptionNode = (Node) xPath.evaluate("description", nodes.item(i), XPathConstants.NODE);
				//Node startTimeNode = (Node) xPath.evaluate("startTime", nodes.item(i), XPathConstants.NODE);
				//Node stopTimeNode = (Node) xPath.evaluate("stopTime", nodes.item(i), XPathConstants.NODE);
				
				//Venue info
				Node venueIDNode = (Node) xPath.evaluate("venue_id", nodes.item(i), XPathConstants.NODE);
				Node venueNameNode = (Node) xPath.evaluate("venue_name", nodes.item(i), XPathConstants.NODE);
				Node venueNode = (Node) xPath.evaluate("venue_address", nodes.item(i), XPathConstants.NODE);
				Node cityNode = (Node) xPath.evaluate("city_name", nodes.item(i), XPathConstants.NODE);
				Node regionNode = (Node) xPath.evaluate("region_name", nodes.item(i), XPathConstants.NODE);
				Node countryNode = (Node) xPath.evaluate("country_name", nodes.item(i), XPathConstants.NODE);
				Node latitudeNode = (Node) xPath.evaluate("latitude", nodes.item(i), XPathConstants.NODE);
				Node longitudeNode = (Node) xPath.evaluate("longitude", nodes.item(i), XPathConstants.NODE);

				//Gets start date and end date
				String startDate = startTimeNode.getTextContent().split(" ")[0];
				String endDate = startTimeNode.getTextContent().split(" ")[0];
				
				//Get start time and end time
				String startTime = startTimeNode.getTextContent().split(" ")[1];				
				String endTime = startTimeNode.getTextContent().split(" ")[1];

				String venueID = Functions.removeSpecialCharacters(venueNameNode.getTextContent().replaceAll(" ", "_")
						.toLowerCase());
				//Event
				Event event = new Event();
				event.setKey(Functions.removeSpecialCharacters(titleNode.getTextContent()).replaceAll(" ", "_").toLowerCase());
				event.setName(titleNode.getTextContent());
				event.setDescription(descriptionNode.getTextContent());
				//event.addVenue(venueID);
				event.setStartDate(startDate);
				event.setEndDate(endDate);
				event.setStartTime(startTime);
				event.setEndDate(endDate);
								
				//Venue object is created in case this venue doesn't exists.
				Venue venue = new Venue();
				venue.setKey(venueID);
				venue.setEventfulID(venueIDNode.getTextContent());
				
				venue.setAddress(venueNode.getTextContent());
				venue.setCityId(cityNode.getTextContent());
				venue.setRegion(regionNode.getTextContent());
				venue.setCountry(countryNode.getTextContent());
				venue.setLatitude(latitudeNode.getTextContent());
				venue.setLongitude(longitudeNode.getTextContent());
								
				events.put(event.toJSON());
				venues.put(venue.toJSON());
			}
			
			result.put("venues", venues);
			result.put("events", events);
				
		} catch (JSONException e) {
			e.printStackTrace();			
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		
		return result;
	}
*/}
